package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.commons.result.util.ObjectTransfer;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @program: project3csmall
 * @description: 实现类
 * @author: shit_code_builder
 * @create: 2021-06-18 09:24
 **/
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;


    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        Long id = request.getId();
        Item item = itemMapper.selectByPrimaryKey(id);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("productId","id");
        hashMap.put("salePrice","price");
        hashMap.put("productName","title");
        hashMap.put("subTitle","sellPoint");
        hashMap.put("limitNum","num");
        hashMap.put("productImageBig","imageBig");
        ProductDetailDto change =null;
        try {
            change = ObjectTransfer.change(item, Item.class, ProductDetailDto.class,hashMap);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        change.setProductImageSmall(Arrays.asList(item.getImages()));
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        change.setDetail(itemDesc.getItemDesc());
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        productDetailResponse.setProductDetailDto(change);
        productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        PageHelper.startPage(request.getPage(),request.getSize());

        Example example = new Example(Item.class);
       if ("-1".equals(request.getSort())){
           example.setOrderByClause("price desc");
       }else if ("1".equals(request.getSort())){
           example.setOrderByClause("price asc");
       }else {
           example.setOrderByClause("id");
       }

        Example.Criteria cid = example.createCriteria().andEqualTo("cid", request.getCid());
       if (request.getPriceGt()!=null&&request.getPriceLte()!=null){
          cid.andBetween("price",request.getPriceGt(),request.getPriceLte());
       }else if (request.getPriceGt()!=null){
           cid.andGreaterThanOrEqualTo("price",request.getPriceGt());
       }else if (request.getPriceLte()!=null){
           cid.andLessThanOrEqualTo("price",request.getPriceLte());
       }

        List<Item> items = itemMapper.selectByExample(example);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("productId","id");
        hashMap.put("salePrice","price");
        hashMap.put("productName","title");
        hashMap.put("picUrl","imageBig");
        hashMap.put("subTitle","sellPoint");

        ArrayList<ProductDto> productDtos = new ArrayList<>();
        for (Item item : items) {
            ProductDto change = ObjectTransfer.change(item, Item.class, ProductDto.class, hashMap);
            productDtos.add(change);
        }

        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setProductDtoList(productDtos);
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);
        long total =itemPageInfo.getTotal();
        allProductResponse.setTotal(total);



        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();
        try {
            //热门推荐的id固定是6？？？
            Panel panel = panelMapper.selectByPrimaryKey(6);
            PanelDto panelDto = contentConverter.panen2Dto(panel);
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panelDto.getId());
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);
            Set<PanelDto> panelDtoSet = new HashSet<>();
            panelDtoSet.add(panelDto);
            response.setPanelContentItemDtos(panelDtoSet);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getCode());
        } catch (Exception e) {
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getCode());
        }
        return response;
    }
}
